﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Public Class SharesEx
    Shared Sub New()
        Try
            Dim aSelectColumns As String() = {"Id", "Recursive", "GroupMode", "ItemTypes", "Name", "Paths", "SecurityGroupIds"}
            ObjectPersister.DefineObject(GetType(Entities.Share), "Shares", aSelectColumns)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Share), "Paths", AddressOf PathsFieldHandler)
            ObjectPersister.AddCustomFieldHandler(GetType(Entities.Share), "SecurityGroupIds", AddressOf SecurityGroupIdsFieldHandler)

        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub
    Private Sub New()
    End Sub

    Private Shared Sub PathsFieldHandler(ByVal obj As Object, ByVal value As Object)
        Dim clsObj As Entities.Share = CType(obj, Entities.Share)
        If value Is DBNull.Value Then
            clsObj.Paths = New Generic.List(Of String)
        Else
            clsObj.Paths = Framework.Serialization.FromDBSeparatorString(CStr(value))
        End If
    End Sub
    Private Shared Sub SecurityGroupIdsFieldHandler(ByVal obj As Object, ByVal value As Object)
        Dim clsObj As Entities.Share = CType(obj, Entities.Share)
        If Not value Is DBNull.Value Then
            Dim strVal As String = CStr(value)
            If Not String.IsNullOrEmpty(strVal) Then
                clsObj.SecurityGroupIds = Framework.Serialization.FromDBSeparatorStringIntegersHashed(strVal)
            End If
        End If
        If clsObj.SecurityGroupIds Is Nothing Then clsObj.SecurityGroupIds = New Generic.HashSet(Of Integer)
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Share)
        Return ObjectPersister.GetAll(Of Entities.Share)()
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Share
        Return ObjectPersister.GetOne(Of Entities.Share)(String.Format("Id={0}", id))
    End Function

    Public Shared Sub Save(ByVal share As Entities.Share)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Save(share, t)
            t.Complete()
        End Using
    End Sub

    Public Shared Sub Save(ByVal share As Entities.Share, ByVal transactionScope As TransactionScope)
        Dim clsCmd As New SaveCommand(Of Integer)("Shares", "Id", share.Id, 0)
        clsCmd.AddParam("Recursive", share.Recursive, False)
        clsCmd.AddParam("GroupMode", share.GroupMode, False)
        clsCmd.AddParam("ItemTypes", share.ItemTypes, False)
        clsCmd.AddParam("Name", share.Name, False)
        clsCmd.AddParam("Paths", Framework.Serialization.ToDBSeparatorString(share.Paths, "?"c), True)
        clsCmd.AddParam("SecurityGroupIds", Framework.Serialization.ToDBSeparatorString(share.SecurityGroupIds, ";"c), False)

        share.Id = clsCmd.Save(transactionScope)
    End Sub


    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        Dim strSql As String

        strSql = "DELETE FROM Shares WHERE Id = @ShareId"
        If Environment.ExecuteNonQuery(strSql, transactionScope, New SqlCeParameter("@ShareId", id)) <> 1 Then
            Throw New ApplicationException("No row was deleted!")
        End If
    End Sub

    Public Shared Function GetAllAsInfos() As Generic.List(Of Entities.Infos.ShareInfo)
        Dim strSql As String = "SELECT Shares.Id, Shares.Name, Shares.ItemTypes, SUM(SharedItems.TotalSize) AS TotalSize, COUNT(SharedItems.Id) AS TotalItems " & _
                               "FROM Shares RIGHT JOIN SharedItems " & _
                               "ON Shares.Id = SharedItems.ShareId " & _
                               "GROUP BY Shares.Id, Shares.Name, Shares.ItemTypes"
        Return Environment.SqlToInfoObjects(Of Entities.Infos.ShareInfo)(strSql)
    End Function
End Class
